var GameData = require("GameData");
var PrefabLoader = require('PrefabLoader');
var Observer = require('Observer');

cc.Class({
    extends: Observer,

    properties: {
        nameLabel: {default: null, displayName: "玩家名字", type: cc.Label},
        idLabel: {default: null, displayName: "身份id", type: cc.Label},
        goldLabel: {default: null, displayName: "金币", type: cc.Label},
        userIcon: {default: null, displayName: "玩家头像", type: cc.Sprite},
        cardNum: {default: null, displayName: "房卡数量", type: cc.Label},

        addNode: {default: null, displayName: "添加节点", type: cc.Node},
        setPrefab: {default: null, displayName: "设置界面", type: cc.Prefab},
        joinRoomPrefab: {default: null, displayName: "加入房间", type: cc.Prefab},
        createRoomPrefab: {default: null, displayName: "创建房间", type: cc.Prefab},
        buyCardPrefab: {default: null, displayName: "购买房卡", type: cc.Prefab},
        sharePrefab: {default: null, displayName: "分享", type: cc.Prefab},
        rulePrefab: {default: null, displayName: "规则", type: cc.Prefab},
        aboutPrefab: {default: null, displayName: "关于", type: cc.Prefab},

    },

    // use this for initialization
    onLoad: function () {
        this._initMsg();
        var name = GameData.getUserName();
        this.nameLabel.string = name;

        this.idLabel.string = GameData.getUserID();
        var gold = GameData.getUserGold();
        this.goldLabel.string = "金币: " + gold.toString();
    },
    _onMsg(msg, data){
        if (msg == MJNetMsg.recv.CreateRoom.msg) {
            this.addNode.destroyAllChildren();
            if (data.result) {
                console.log("创建房间成功,进入房间中...!");
                cc.director.loadScene("GameScene");
                PrefabLoader.addLoadingLayer(this.addNode);
            } else {
                PrefabLoader.addTipsDialog("创建房间失败!");
            }
        }else if(msg == MJNetMsg.recv.JoinRoom.msg){
            this.addNode.destroyAllChildren();
            if (data.result) {
                console.log("加入房间成功,进入房间中...!");
                cc.director.loadScene("GameScene");
                PrefabLoader.addLoadingLayer(this.addNode);
            } else {
                PrefabLoader.addTipsDialog("加入房间失败!");
            }
        }else if(msg == GameLocalMsg.Center.OnClickAbout){
            this.onClickAbout();
        }
    },
    _getMsgList(){
        return [
            MJNetMsg.recv.CreateRoom.msg,
            MJNetMsg.recv.JoinRoom.msg,
            GameLocalMsg.Center.OnClickAbout,
        ];
    },


    onClickAbout(){
        var layer = cc.instantiate(this.aboutPrefab);
        this.addNode.addChild(layer);
    },
    // 加入房间
    onClickJoinRoom(){
        var layer = cc.instantiate(this.joinRoomPrefab);
        this.addNode.addChild(layer);
    },
    // 创建房间
    onClickCreateRoom(){
        var layer = cc.instantiate(this.createRoomPrefab);
        this.addNode.addChild(layer);
    },
    // 购买房卡
    onClickBuyRoomCard(){
        var layer = cc.instantiate(this.buyCardPrefab);
        this.addNode.addChild(layer);
    },
    // 战绩
    onClickFightStatus(){

    },
    // 规则
    onClickRules(){
        var layer = cc.instantiate(this.rulePrefab);
        this.addNode.addChild(layer);
    },
    // 设置
    onClickSet(){
        var layer = cc.instantiate(this.setPrefab);
        this.addNode.addChild(layer);
    },
    // 点击角色
    onClickRole(){

    },
    onClickShare(){
        var layer = cc.instantiate(this.sharePrefab);
        this.addNode.addChild(layer);
    },

});
